<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio v-for="(type, index) in dataForm.typeList" :label="index" :key="index">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="标题名" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题名"></el-input>
      </el-form-item>
      <el-form-item label="授权标识" prop="permission">
        <el-input v-model="dataForm.permission" placeholder="权限名，通常据此来筛选用户允许访问的路由"></el-input>
      </el-form-item>
      <el-form-item label="name" prop="routerName">
        <el-input v-model="dataForm.routerName" placeholder="需与页面组件的name一致"></el-input>
      </el-form-item>
      <el-form-item label="上级name" prop="parentName" v-if="dataForm.type === 1">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="routerId"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
          <el-input v-model="dataForm.parentName" slot="reference" :readonly="true" placeholder="点击选择上级菜单" class="menu-list__input"></el-input>
        </el-popover>
        </el-form-item>
      <!-- <el-form-item label="上级ID" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder="上级ID"></el-input>
      </el-form-item> -->
      <el-form-item label="url访问路径" prop="url">
        <el-input v-model="dataForm.url" placeholder="url访问路径"></el-input>
      </el-form-item>

      <el-form-item label="组件名" prop="component">
        <el-row>
          <el-col :span="22" v-if="dataForm.type === 1">
            <el-input v-model="dataForm.component" placeholder="组件名，由前端确定后添加" ></el-input>
          </el-col>
          <el-col :span="2" class="icon-list__tips">
            <el-tooltip placement="top" effect="light">
              <div slot="content">组件名为routerMapComponents中的key,若为根路径，则只能是layoutHeaderAside</div>
              <i class="el-icon-warning"></i>
            </el-tooltip>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="组件对应路径" prop="componentPath" v-if="dataForm.type === 1">
        <el-input v-model="dataForm.componentPath" placeholder="组件对应路径，由前端确定后添加"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序"></el-input-number>
      </el-form-item>
      <el-form-item label="是否锁定" prop="isLock">
        <el-switch
            v-model="dataForm.isLock">
        </el-switch>
      </el-form-item>
      <el-form-item label="是否缓存" prop="cache">
          <el-switch
            v-model="dataForm.cache">
        </el-switch>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="上次更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="数据创建者" prop="creator">
        <el-input v-model="dataForm.creator" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="数据修改者" prop="updater">
        <el-input v-model="dataForm.updater" :placeholder="dataForm.uuid" :disabled="true"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { reqUpdate, reqSave, reqInfo, reqList } from '@api/userrouter.api.fetch'
import util from '@/libs/util.js'
import dayjs from 'dayjs'
export default {
  props: {
    isUpdate: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      visible: false,
      dataForm: {
        routerId: 0,
        parentId: '',
        url: '',
        routerName: '',
        title: '',
        permission: '',
        component: '',
        componentPath: '',
        sort: '',
        isLock: false,
        cache: false,
        remark: '',
        createTime: '',
        updateTime: '',
        creator: '',
        updater: '',
        uuid: '',
        typeList: ['根', '路径'],
        type: 1,
        parentName: ''
      },
      dataRule: {
        url: [
          { required: true, message: 'url访问路径不能为空', trigger: 'blur' }
        ],
        routerName: [
          { required: true, message: '需与页面组件的name一致不能为空', trigger: 'blur' }
        ],
        parentName: [
          { required: true, message: '上级路径不能为空', trigger: 'change' }
        ],
        title: [
          { required: true, message: '标题名不能为空', trigger: 'blur' }
        ],
        permission: [
          { required: true, message: '授权标识，通常据此来筛选用户允许访问的路由。不能为空', trigger: 'blur' }
        ]
      },
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      },
      menuList: []
    }
  },
  mounted () {
    this.uuid = util.cookies.get('uuid')
  },
  methods: {
    init (id) {
      reqList().then(res => {
        this.menuList = res.data
      }).then(() => {
        this.dataForm.routerId = id || 0
        this.dataForm.type = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.routerId) {
            reqInfo(this.dataForm.routerId).then(res => {
              if (res) {
                let data = res.data

                this.dataForm.parentId = data.parentId
                this.dataForm.url = data.url
                this.dataForm.routerName = data.routerName
                this.dataForm.title = data.title
                this.dataForm.permission = data.permission
                this.dataForm.component = data.component
                this.dataForm.componentPath = data.componentPath
                this.dataForm.sort = data.sort
                this.dataForm.isLock = data.isLock
                this.dataForm.cache = data.cache
                this.dataForm.remark = data.remark
                this.dataForm.createTime = dayjs(data.createTime).format('YYYY-M-D HH:mm:ss')
                this.dataForm.updateTime = dayjs(data.updateTime).format('YYYY-M-D HH:mm:ss')
                this.dataForm.creator = data.creator
                this.dataForm.updater = data.updater
                this.dataForm.type = data.parentId ? 1 : 0
                this.menuListTreeSetCurrentNode()
              }
            })
          } else {
            // 新增
            this.menuListTreeSetCurrentNode()
          }
        })
      })
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle (data, node) {
      debugger
      this.dataForm.parentId = data.routerId
      this.dataForm.parentName = data.name
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode () {
      this.$nextTick(() => {
        if (this.$refs.menuListTree) {
          this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
          let node = this.$refs.menuListTree.getCurrentNode()
          this.dataForm.parentName = node.name
          this.dataForm.parentId = node.routerId
        }
      })
      // this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['title']
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          var needToSave = {
            'routerId': this.dataForm.routerId || undefined,
            'parentId': this.dataForm.parentId || undefined,
            'url': this.dataForm.url,
            'routerName': this.dataForm.routerName,
            'title': this.dataForm.title,
            'permission': this.dataForm.permission,
            'component': (this.dataForm.type === 0) ? 'layoutHeaderAside' : this.dataForm.component,
            'componentPath': (this.dataForm.type === 0) ? 'layout/header-aside/layout' : this.dataForm.componentPath,
            'sort': this.dataForm.sort,
            'isLock': this.dataForm.isLock,
            'cache': this.dataForm.cache,
            'remark': this.dataForm.remark,
            // 'createTime': this.dataForm.createTime,
            // 'updateTime': this.dataForm.updateTime,
            'creator': this.dataForm.creator || this.uuid,
            'updater': this.uuid
          }
          var mAction = this.isUpdate ? reqUpdate : reqSave
          mAction(needToSave).then(res => {
            if (res) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$router.push('router')
                  // this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .mod-menu {
    .menu-list__input,
    .icon-list__input {
       > .el-input__inner {
        cursor: pointer;
      }
    }
    &__icon-popover {
      width: 458px;
      overflow: hidden;
    }
    &__icon-inner {
      width: 478px;
      max-height: 258px;
      overflow-x: hidden;
      overflow-y: auto;
    }
    &__icon-list {
      width: 458px;
      padding: 0;
      margin: -8px 0 0 -8px;
      > .el-button {
        padding: 8px;
        margin: 8px 0 0 8px;
        > span {
          display: inline-block;
          vertical-align: middle;
          width: 18px;
          height: 18px;
          font-size: 18px;
        }
      }
    }
    .icon-list__tips {
      font-size: 18px;
      text-align: center;
      color: #e6a23c;
      cursor: pointer;
    }
  }
</style>
